<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: aqua;
      position: absolute;
      left: 0;
      top: 0;
    }
  </style>
  <body>
    <div></div>
    <script>
      var oDiv = document.getElementsByTagName('div')[0];
      oDiv.onmousedown = function (e) {
        var event = e || window.event;
        var disX = event.clientX - this.offsetLeft;
        var disY = event.clientY - this.offsetTop;
        var self = this;
        document.onmousemove = function (e) {
          var event = e || window.event;
          var newLeft = event.clientX - disX;
          var newTop = event.clientY - disY;

          self.style.top = newTop + 'px';
          self.style.left = newLeft + 'px';
        };
        document.onmouseup = function () {
          document.onmousemove = null;
        };
      };
    </script>
  </body>
</html>
